<html>
	<head>
		<title>ISOGAME - test 1</title>
		<script language="javascript" src="js/isogame.js"></script>
		<script>
			var id = 0;
			function new_tile(){
				isogame.EventManager.unregister( s );

				s = new isogame.Sprite("test_"+id, "w");
				id=id+1;
				s.src['w'] = "img/pj.png";
				s.dim_z = 30;
				s.dim_x = 15;
				s.dim_y = 15;
				s.setXYZ(1,1,1);
				s.notify = pj_notify;
				isogame.EventManager.register( s );
				scene.addObject( s );
				s.draw("screen");
			}

			window.onload=function(){
				document.getElementById("output").value="";
				// Create a Sprite
				s = new isogame.Sprite("test_"+id, "w");
				id=id+1;
				s.src['w'] = "img/tile_black.gif";
				s.dim_z = 9;
				s.dim_y = 17;
				s.dim_x = 17;
				s.notify = pj_notify;
				s.setXYZ(1,1,10);
				isogame.EventManager.register( s );

				// Draw a 10x10 lattice
				scene = new isogame.Scene();
				for(var x=0; x< 180; x+=18)
					for(var y=0; y< 180; y+=18) {
						var tile = new isogame.Tile( x+","+y, "", x,y);
						//tile.z = Math.round( 10*Math.random() );
						tile.z = 1;
						tile.dim_x=18;
						tile.dim_y=18;
						scene.addTile( tile );
					}

				scene.addObject( s );
				scene.draw("screen");
				
				isogame.mainloop( principal, 100 );
			};

			function principal(){
				isogame.EventManager.process();

				var nextCube = s.getCube();
				nextCube.z--;
				var obs=scene.whatsInCube(nextCube);
				if( obs.length == 0 ) s.setCube( nextCube );
				s.update();
			};

			function pj_notify(ev){
				var speed=9;
				if(ev.type == "keydown"){
					var key = ev.originalEvent.keychar;
					var nextCube = s.getCube();
					var new_x = s.x;
					var new_y = s.y;
					var new_z = s.z;

					if(key=="UP")    nextCube.x-=speed;
					if(key=="DOWN")  nextCube.x+=speed;
					if(key=="LEFT")  nextCube.y-=speed;
					if(key=="RIGHT") nextCube.y+=speed;

						var obs=scene.whatsInCube(nextCube);
						if( obs.length == 0 ) s.setCube( nextCube );
						return;
				}
			}
		</script>
	</head>
	<body>
	<fieldset>
	<legend>Sprite controls</legend>
	<p> Use cursors to move.</p>
	<table>
		<tr>
			<td><input type="button" value="y--" onclick="s.setXYZ(s.x, s.y-1);"/></td>
			<td><input type="button" value="x--" onclick="s.setXYZ(s.x-1);"/></td>
			<td><input type="button" value="z++" onclick="s.setXYZ(s.x, s.y, s.z+10);"/></td>
		</tr>
		<tr>
			<td><input type="button" value="x++" onclick="s.setXYZ(s.x+1);"/></td>
			<td><input type="button" value="y++" onclick="s.setXYZ(s.x, s.y+1);"/></td>
			<td><input type="button" value="z--" onclick="s.setXYZ(s.x, s.y, s.z-10);"/></td>
		</tr>
		<tr>
			<td><input type="button" value="draw" onclick="s.draw('screen')"/></td>
			<td><input type="button" value="erase" onclick="s.erase()"/></td>
			<td><input type="button" value="new" onclick="new_tile()"/></td>
		</tr>
	</table>
	</legend>
	<div id="screen"></div>
	<textarea id="output" cols="50" rows="10"></textarea>
	</body>
</html>
